package com.example.authoritymanageaopproject.aop;

import android.util.Log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * 类名：AuthorityAspect<br></br>
 * 主要功能：<br></br>
 * 创建日期：05/09/2017  17:12
 */
@Aspect
public class AuthorityAspect {
    private static final int ACCESS_MIN_LEVEL = 3;
    private static final String POINTCUT_METHOD =
            "execution(@com.example.authoritymanageaopproject.aop.AuthorityAnn * *(..))";

    @Pointcut(POINTCUT_METHOD)
    public void annAuthority() {

    }

    @Around("annAuthority()")
    public Object doBiz(ProceedingJoinPoint point)
            throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        AuthorityAnn annotation = signature.getMethod().getAnnotation(AuthorityAnn.class);
        int level = annotation.value();

        if (level < ACCESS_MIN_LEVEL) {
            Log.d("AuthorityAspect", "权限不足");
            return null;
        }

        Object obj = null;
        try {
            obj = point.proceed();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return obj;
    }
}
